package com.byl.common.interceptor;

import com.byl.modules.sys.service.LoggerService;
import com.byl.sdk.utils.NetworkUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2020/1/2.
 */
@Component
public class LoggerInterceptor implements HandlerInterceptor {

    @Autowired
    LoggerService loggerService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        String path = request.getContextPath() + request.getServletPath();
        String ip= NetworkUtil.getIpAddress(request);
        Map map=new HashMap();
        map.put("ip",ip);
        map.put("path",path);
        loggerService.save(map);
    }
}
